package net.oschina.simpleplus.erpc.consumer;

import java.util.concurrent.ThreadPoolExecutor;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import net.oschina.simpleplus.erpc.api.ERpcContainer;
import net.oschina.simpleplus.erpc.core.ERpcRequestEntity;

public class ERpcHandler extends SimpleChannelInboundHandler<ERpcRequestEntity>{
	
	private ThreadPoolExecutor executor ;
	private ERpcContainer eRpcContainer;
	public ERpcHandler(ThreadPoolExecutor executor, ERpcContainer eRpcContainer) {
		this.executor = executor;
		if(this.executor == null){
			throw new RuntimeException("不允许内部创建线程池");
			//executor = ThreadPoolExecutorFactory.create(1, 2, 2, TimeUnit.MINUTES, capacity, "ERpcHandler-pool-");
		}
		this.eRpcContainer = eRpcContainer;
	}


	@Override
	protected void channelRead0(ChannelHandlerContext ctx, ERpcRequestEntity msg) throws Exception {
		executor.submit(new ConsumerTask(ctx, msg, eRpcContainer));
	}

	

}
